package com.zyd.controller.enclosure;

import com.zyd.constants.ErrorDef;
import com.zyd.exception.BusinessException;
import com.zyd.io.page.PageBean;
import com.zyd.io.vo.page.MultiQueryPageVO;
import com.zyd.io.vo.res.ResBody;
import com.zyd.pojo.Enclosure;
import com.zyd.service.enclosure.EnclosureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: caicaicainiaoa
 * @Date: 2019/4/2  11:40
 * <p>
 * 电子围栏控制层
 */
@RestController
@RequestMapping("/enclosure")
public class EnclosureController {
    @Autowired
    private EnclosureService service;

    /**
     * @param map
     * @return
     */
    @PostMapping(value = "/add", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody add(@RequestBody Map<String, Object> map) {
        try {
            service.save(map);
            return new ResBody(ErrorDef.SUCCESS, "添加围栏成功");
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "请求失败");
        }
    }

    /**
     * 根据id查询一个
     *
     * @param map
     * @return
     */
    @PostMapping(value = "/findOne", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findOne(@RequestBody Map<String, Object> map) {
        try {
            Map resultMap= service.findOneById(map);
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询围栏成功");
            resBody.setData(resultMap);
            return resBody;

        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "请求失败");
        }
    }

    /**
     * 修改
     *
     * @param map
     * @return
     */
    @PostMapping(value = "/update", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody update(@RequestBody Map<String, Object> map) {
        try {
            service.save(map);
            return new ResBody(ErrorDef.SUCCESS, "修改围栏成功");

        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "请求失败");
        }
    }
    /**
     * 删除
     *
     * @param map
     * @return
     */
    @PostMapping(value = "/delete", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody delete(@RequestBody Map<String, Object> map) {
        try {
            service.delete(map);
            return new ResBody(ErrorDef.SUCCESS, "删除围栏成功");

        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBody(ErrorDef.SYSTEM_ERROR, "请求失败");
        }
    }

    /**
     *  分页查询
     *
     * @param MultiQueryPageVO
     * @return
     */
    @PostMapping(value = "/findList", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findEnclosurePage(@RequestBody MultiQueryPageVO MultiQueryPageVO) {
        try {

            PageBean<Enclosure> page = service.findEnclosurePage(MultiQueryPageVO);

            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询车辆列表成功");
            resBody.setData(page);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查看车辆列表失败");
        }
    }


}
